<?php

/*
 * This file is part of the sfSAMLplugin package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 * (c) 2010      Théophile Helleboid <t.helleboid@iariss.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfSAMLPlugin configuration.
 * 
 * @package    sfSAMLPlugin
 * @subpackage sfSAMLAuth
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @author     Théophile Helleboid <t.helleboid@iariss.fr>
 */
class BasesfSAMLAuthActions extends sfActions{

    public function executeSignin($request){
        // Create SimpleSAML module
        $simpleSAMLAuth = new SimpleSAML_Auth_Simple('default-sp');

        // If the user is authenticated from the IdP
        if($simpleSAMLAuth->isAuthenticated()){
            return $this->redirect('@authenticated');
        }else{
            $this->executeRemove_session_local_saml();

            if($request->isXmlHttpRequest()){
                $this->getResponse()->setHeaderOnly(true);
                $this->getResponse()->setStatusCode(401);
                return sfView::NONE;
            }

            $this->url_idp = $simpleSAMLAuth->login();
        }

    }

    public function executeSignout($request){
        $signoutUrl = sfConfig::get('app_sf_guard_plugin_success_signout_url');

        $simpleSAMLAuth = new SimpleSAML_Auth_Simple('default-sp');
        $simpleSAMLAuth->logout($this->generateUrl('' != $signoutUrl ? $signoutUrl : 'logout_aplication'), array(), true);
        // Nothing happen after there

        $this->redirect('' != $signoutUrl ? $signoutUrl : 'logout_aplication');
    }

    public function executeSecure($request){
        $this->getResponse()->setStatusCode(403);
    }

    public function executePassword($request){
        throw new sfException('This method is not yet implemented.');
    }

    public function executeRemove_session_local_saml(){
        $this->getUser()->getAttributeHolder()->remove('ses_user_id');
        $this->getUser()->getAttributeHolder()->remove('ses_user_nombre');
        $this->getUser()->getAttributeHolder()->remove('ses_user_ape_pat');
        $this->getUser()->getAttributeHolder()->remove('ses_user_ape_mat');
        $this->getUser()->getAttributeHolder()->remove('ses_user_correo');
        $this->getUser()->getAttributeHolder()->remove('ses_tipo_user');
        $this->getUser()->getAttributeHolder()->remove('ses_acuerdo_id');
        $this->getUser()->getAttributeHolder()->remove('ses_acta_id');
        $this->getUser()->getAttributeHolder()->remove('ses_categoria');
        $this->getUser()->getAttributeHolder()->remove('ses_lst_documento');
        $this->getUser()->getAttributeHolder()->remove('ses_paquete_id');
        $this->getUser()->setAuthenticated(false);
        $this->getUser()->getAttributeHolder()->clear();
    }


}